//
//  LDY_ArticleDetailViewController.m
//  ProjectOfBStage
//
//  Created by dlios on 15/11/14.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "LDY_ArticleDetailViewController.h"
#import "Network.h"
#import "LDY_ReadDetailModel.h"
#import "LDY_ReadDetailTableViewCell.h"
#import "LDY_CycleViewController.h"
#import "LDY_ReadContectViewController.h"
#import <MJRefresh.h>

@interface LDY_ArticleDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray *newlistArr;

@property (nonatomic, retain) NSMutableArray *newmainArr;

@property (nonatomic, retain) NSMutableArray *hotListArr;

@property (nonatomic, retain) NSMutableArray *hotMainArr;

@property (nonatomic, retain) UITableView *newtableView;

@property (nonatomic, retain) UITableView *hottableView;

@property (nonatomic, retain) UIButton *newbutton;

@property (nonatomic, retain) UIButton *hotbutton;

@property (nonatomic, retain) UIScrollView *scrollView;

@property (nonatomic, assign) NSInteger newPage;

@property (nonatomic, assign) NSInteger hotPage;

@property (nonatomic, assign) NSInteger newMaxPageId;

@property (nonatomic, assign) NSInteger hotMaxPageId;

@end

@implementation LDY_ArticleDetailViewController

- (void)dealloc
{
    [_type release];
    [_newlistArr release];
    [_newmainArr release];
    [_hotListArr release];
    [_hotMainArr release];
    [_newtableView release];
    [_hottableView release];
    [_scrollView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.newlistArr = [NSMutableArray array];
        self.newmainArr = [NSMutableArray array];
        self.hotListArr = [NSMutableArray array];
        self.hotMainArr = [NSMutableArray array];
        self.newPage = 0;
        self.hotPage = 0;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    /* 创建返回按钮 */
    [self.barViewLeftButton setImage:[UIImage imageNamed:@"iconfont-xiangzuojiantou"] forState:UIControlStateNormal];
    [self.barViewLeftButton addTarget:self action:@selector(barViewLeftButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self createScrollView];
    
    [self createButton];
    
    [self newDataHandle];
    
    [self hotDataHandle];
    
    [self creatNewTableView];
    
    [self createHotTableView];
}

- (void)createButton
{
    self.newbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.barView addSubview:self.newbutton];
    self.newbutton.frame = CGRectMake(KWIDTH * 250, KHEIGHT * 18, KWIDTH * 23, KWIDTH * 20);
    self.newbutton.layer.cornerRadius = 3;
    [self.newbutton setBackgroundColor:[UIColor blackColor]];
    [self.newbutton setTitle:@"NEW" forState:UIControlStateNormal];
    self.newbutton.titleLabel.font = [UIFont systemFontOfSize:9 * KWIDTH weight:10 * KWIDTH];
    [self.newbutton addTarget:self action:@selector(newbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    self.hotbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.barView addSubview:self.hotbutton];
    self.hotbutton.frame = CGRectMake(KWIDTH * 300, KHEIGHT * 18, KWIDTH * 22, KWIDTH * 20);
    self.hotbutton.layer.cornerRadius = 3;
    [self.hotbutton setBackgroundColor:[UIColor blackColor]];
    [self.hotbutton setTitle:@"HOT" forState:UIControlStateNormal];
    self.hotbutton.alpha = 0.3;
    self.hotbutton.titleLabel.font = [UIFont systemFontOfSize:9 * KWIDTH weight:10 * KWIDTH];
    [self.hotbutton addTarget:self action:@selector(hotbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

/* 返回事件 */
- (void)barViewLeftButtonClicked
{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
}

- (void)newbuttonClicked:(UIButton *)button
{
    self.newbutton.alpha = 1.0;
    self.hotbutton.alpha = 0.3;
    
    self.scrollView.contentOffset = CGPointMake(0, 0);
}

-(void)hotbuttonClicked:(UIButton *)button
{
    self.newbutton.alpha = 0.3;
    self.hotbutton.alpha = 1.0;
    
    self.scrollView.contentOffset = CGPointMake(KWIDTH * 375, 0);
}

- (void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 46 * KHEIGHT, KWIDTH * 375, KHEIGHT * 667 - 45 * KHEIGHT)];
    
    self.scrollView.backgroundColor = [UIColor grayColor];
    
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    
    [self.view addSubview:self.scrollView];
    
    /* 整页滚动 */
    self.scrollView.pagingEnabled = YES;
    
    /* 视图左上角距离原点的偏移量 */
    self.scrollView.contentOffset = CGPointMake(0, 0);
    
    /* 是否显示垂直滚动条, 默认值为YES */
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    /* 边界是否回弹 */
    self.scrollView.bounces = NO;
    
    self.scrollView.delegate = self;

    [self.scrollView release];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([[NSString stringWithFormat:@"%@",[scrollView class]] isEqualToString:@"UIScrollView"]){
    
    if (scrollView.contentOffset.x == KWIDTH * 375) {
        
            self.newbutton.alpha = 0.3;
            self.hotbutton.alpha = 1.0;
        
    }else if(scrollView.contentOffset.x == 0) {
        
            self.newbutton.alpha = 1.0;
            self.hotbutton.alpha = 0.3;
        }
    }
}

- (void)newDataHandle
{
    NSString *body = [NSString stringWithFormat:@"auth=&client=1&deviceid=7F95D47F-BF62-4528-B18E-EE921FFC5323&limit=10&sort=addtime&start=0&typeid=%@&version=3.0.6", self.type];
    
    [Network networkPOSTRequestWithURL:@"http://api2.pianke.me/read/columns_detail" body:body pageUniquenessIdentifier:body result:^(id result) {
        NSDictionary *dic = result;
        
        self.newlistArr = [[dic objectForKey:@"data"] objectForKey:@"list"];
        
        for (NSDictionary *dic in self.newlistArr) {
            
            LDY_ReadDetailModel *model = [LDY_ReadDetailModel modelWithDictionary:dic];
            
            [self.newmainArr addObject:model];
        }
        if (self.newmainArr.count > 0) {
            
            [self.newtableView reloadData];
        }
    }];
}

- (void)hotDataHandle
{
    NSString *body = [NSString stringWithFormat:@"auth=&client=1&deviceid=7F95D47F-BF62-4528-B18E-EE921FFC5323&limit=10&sort=hot&start=0&typeid=%@&version=3.0.6", self.type];
    
    [Network networkPOSTRequestWithURL:@"http://api2.pianke.me/read/columns_detail" body:body pageUniquenessIdentifier:body result:^(id result) {
        NSDictionary *dic = result;
        
        self.hotListArr = [[dic objectForKey:@"data"] objectForKey:@"list"];
        
        for (NSDictionary *dic in self.hotListArr) {
            
            LDY_ReadDetailModel *model = [LDY_ReadDetailModel modelWithDictionary:dic];
            
            [self.hotMainArr addObject:model];
        }
        if (self.hotMainArr.count > 0) {
            
            [self.hottableView reloadData];
        }
    }];
}

- (void)newHeaderFreshing
{
    [self.newmainArr removeAllObjects];
    
    [self newDataHandle];
    
    [self.newtableView.header endRefreshing];
}

- (void)newFooterFreshing
{
    self.newPage = self.newPage + 10;
    
    NSString *body = [NSString stringWithFormat:@"auth=&client=1&deviceid=7F95D47F-BF62-4528-B18E-EE921FFC5323&limit=10&sort=addtime&start=%ld&typeid=%@&version=3.0.6", (long)self.newPage, self.type];
    
    [Network networkPOSTRequestWithURL:@"http://api2.pianke.me/read/columns_detail" body:body pageUniquenessIdentifier:body result:^(id result) {
        NSDictionary *dic = result;
        
        self.newlistArr = [[dic objectForKey:@"data"] objectForKey:@"list"];
        
        for (NSDictionary *dic in self.newlistArr) {
            
            LDY_ReadDetailModel *model = [LDY_ReadDetailModel modelWithDictionary:dic];
            
            [self.newmainArr addObject:model];
        }
        if (self.newmainArr.count > 0) {
            
            [self.newtableView reloadData];
        }
        
    }];
    
    [self.newtableView.footer endRefreshing];
    
    if (self.newMaxPageId) {
        NSLog(@"%ld, %ld", (long)self.newPage, (long)self.newMaxPageId);
        self.newtableView.footer.hidden = YES;
    }
}

- (void)hotHeaderFreshing
{
    [self.hotMainArr removeAllObjects];
    
    [self hotDataHandle];
        
    [self.hottableView.header endRefreshing];
}

- (void)hotFooterFreshing
{
    self.hotPage += 10;
    
    NSString *body = [NSString stringWithFormat:@"auth=&client=1&deviceid=7F95D47F-BF62-4528-B18E-EE921FFC5323&limit=10&sort=hot&start=%ld&typeid=%@&version=3.0.6", (long)self.hotPage, self.type];
    
    [Network networkPOSTRequestWithURL:@"http://api2.pianke.me/read/columns_detail" body:body pageUniquenessIdentifier:body result:^(id result) {
        NSDictionary *dic = result;
        
        self.hotListArr = [[dic objectForKey:@"data"] objectForKey:@"list"];
        
        for (NSDictionary *dic in self.hotListArr) {
            
            LDY_ReadDetailModel *model = [LDY_ReadDetailModel modelWithDictionary:dic];
            
            [self.hotMainArr addObject:model];
        }
        
        if (self.hotMainArr.count > 0) {
            
            [self.hottableView reloadData];
        }
        
        [self.hottableView.footer endRefreshing];
    }];
    
    if (self.hotMaxPageId) {
        NSLog(@"%ld, %ld", (long)self.hotPage, (long)self.hotMaxPageId);
        self.hottableView.footer.hidden = YES;
    }
}

- (void)creatNewTableView
{
    self.newtableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH * 375, KHEIGHT * 667 - 45) style:UITableViewStylePlain];
    
    [self.scrollView addSubview:self.newtableView];
    
    self.newtableView.delegate = self;
    self.newtableView.dataSource =  self;
    
    self.newtableView.rowHeight = KHEIGHT * 150;
    
    self.newtableView.tag = 1000;
    
    self.newtableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(newHeaderFreshing)];
    
    self.newtableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(newFooterFreshing)];
    
    [self.newtableView release];
}

- (void)createHotTableView
{
    self.hottableView = [[UITableView alloc] initWithFrame:CGRectMake(KWIDTH * 375, 0, KWIDTH * 375, KHEIGHT * 667 - 45) style:UITableViewStylePlain];
    
    [self.scrollView addSubview:self.hottableView];
    
    self.hottableView.delegate = self;
    self.hottableView.dataSource = self;
    
    self.hottableView.rowHeight = KHEIGHT * 150;
    
    self.hottableView.tag = 2000;
    
    self.hottableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(hotHeaderFreshing)];
    
    self.hottableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(hotFooterFreshing)];
    
    [self.hottableView release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1000) {
        LDY_ReadContectViewController *readContect = [[LDY_ReadContectViewController alloc] init];
        
        LDY_ReadDetailModel *model = [self.newmainArr objectAtIndex:indexPath.row];
        
        readContect.ID = model.ID;
        
        readContect.titleName = model.title;
        
        readContect.img = model.coverimg;
        
        readContect.content = model.content;
        
        [self.navigationController pushViewController:readContect animated:YES];
    }else{
        LDY_ReadContectViewController *readContect = [[LDY_ReadContectViewController alloc] init];
        
        LDY_ReadDetailModel *model = [self.hotMainArr objectAtIndex:indexPath.row];
        
        readContect.ID = model.ID;
        
        [self.navigationController pushViewController:readContect animated:YES];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 1000) {
        if (self.newmainArr.count > 0) {
            
            return self.newmainArr.count;
        }else{
            return 0;
        }
    }else{
        if (self.hotMainArr.count > 0) {
            
            return self.hotMainArr.count;
        }else{
            return 0;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LDY_ReadDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LDY_ReadDetailTableViewCell"];
    if (!cell) {
        
        cell = [[LDY_ReadDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LDY_ReadDetailTableViewCell"];
    }
    if (tableView.tag == 1000) {
        if (self.newmainArr.count > 0) {
            cell.model = [self.newmainArr objectAtIndex:indexPath.row];
        }
    }else{
        if (self.hotMainArr.count > 0) {
            cell.model = [self.hotMainArr objectAtIndex:indexPath.row];
        }
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
